<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .form-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }

        .form-control {
            width: 100%;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }

        .form-control:focus {
            border-color: #007bff;
            outline: none;
        }

        .error-list {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
        }

        .error-list ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .error-field {
            border-color: #dc3545 !important;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-secondary {
            background-color: #6c757d;
            color: white;
            text-decoration: none;
            display: inline-block;
        }

        .messages {
            margin-bottom: 20px;
        }

        .alert {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 10px;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h1>{{ title }}</h1>
    <!-- 显示消息 -->
    {% if messages %}
    <div class="messages">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }}">
            {{ message }}
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <form method="post">
        {% csrf_token %}

        <!-- 手动渲染每个字段 -->
        {% for field in form %}
        <div class="form-group">
            <label for="{{ field.id_for_label }}">{{ field.label }}</label>
            {{ field }}

            <!-- 显示字段错误 -->
            {% if field.errors %}
            <div class="error-list">
                {{ field.errors }}
            </div>
            {% endif %}

            <!-- 显示字段帮助文本 -->
            {% if field.help_text %}
            <small style="color: #6c757d;">{{ field.help_text }}</small>
            {% endif %}
        </div>
        {% endfor %}

        <div>
            <button type="submit" class="btn btn-primary">
                {% if form_type == 'add' %}添加学生{% else %}更新信息{% endif %}
            </button>
            <a href="{% url 'student_list' %}" class="btn btn-secondary">取消返回</a>
        </div>
    </form>
</div>

<script>
    // 自动为有错误的字段添加错误样式
    document.addEventListener('DOMContentLoaded', function () {
        const errorFields = document.querySelectorAll('.errorlist');
        errorFields.forEach(function (errorField) {
            const input = errorField.closest('.form-group').querySelector('input, select, textarea');
            if (input) {
                input.classList.add('error-field');
            }
        });
    });
</script>
</body>
</html>